<?php
namespace app\admin\controller;
use app\index\model\Tag;
use think\Controller;
use think\Hook;
use think\View;
use app\admin\model\Blog;
use app\admin\model\Category;

class Index extends Controller
{
    public function index()
    {
        $list = BLog::paginate(10);
        $view =new View();
        $view->assign('list',$list);
        return $view->fetch('admin/sbadmin/pages/tables');
    }


    public function edit($id=''){
    	
    	$blog=Blog::get($id);
        $view = new View();
        $view->assign('blog',$blog);

		$list=Category::paginate(50);
		$view->assign('catlist',$list);

        return $view->fetch('admin/sbadmin/pages/flot');
    }


    public function update($blog_id){
    	
		$blog_id = input('blog_id');
	    $blog_title= input('blog_title');
	    $blog_author = input('blog_author');
	    $blog_content = input('myEditor');
		$is_top = input('is_top');
		if($blog_id!=''){
	        $blog = Blog::get($blog_id);
		}else{
			$blog=new Blog;
		}
		$blog->blog_title = $blog_title;
	    $blog->blog_author = $blog_author;
	    $blog->blog_content = $blog_content;
		$blog->is_top = $is_top;
		$result = $blog->save();
		if($result){
	        return $this->success('保存成功','admin/index/index');
	    }else if($result===0){
	    	return $this->error('没有进行修改，请重新输入');
	    }else{
	    	return $this->error('保存失败');
	    }

    }

	public function catlist(){

		$list=Category::paginate(50);
		$view=new View();
		$view->assign('catlist',$list);
		return $view->fetch('admin/sbadmin/pages/catlist');

	}

	public function cat_edit($cat_id=''){

		$cat=Category::get($cat_id);
		$view = new View();
		$view->assign('cat',$cat);
		return $view->fetch('admin/sbadmin/pages/morris');

	}

	public function cat_update($cat_id){

		$cat_id=input('cat_id');
		$cat_id1=input('cat_id1');
		$cat_name=input('cat_name');
        $display = input('display');

		if($cat_id!=''){
			$cat = Category::get($cat_id);
		}else{
			$cat=new Category;
		}
		$cat->cid = $cat_id1;
		$cat->cat_name = $cat_name;
        if(is_null($display)){
            $cat->display= 0;
        }else{
			$cat->display=$display;
		}
		$result = $cat->save();
		if($result){
			return $this->success('保存成功','admin/index/catlist');
		}else if($result===0){
			return $this->error('没有进行修改，请重新输入');
		}else{
			return $this->error('保存失败');
		}

    }

    public function taglist(){

        $list=Tag::paginate(50);
        $view=new View();
        $view->assign('taglist',$list);
        return $view->fetch('admin/sbadmin/pages/taglist');

    }

    public function tag_edit($tag_id=''){

        $tag=Tag::get($tag_id);
        $view = new View();
        $view->assign('tag',$tag);
        return $view->fetch('admin/sbadmin/pages/tagedit');
    }


    public function tag_update($tag_id){

        $tag_id=input('tag_id');
        $tag_name=input('tag_name');
        if($tag_id!=''){
            $tag = Tag::get($tag_id);
        }else{
            $tag=new Tag();
        }
        $tag->tname = $tag_name;
        $result = $tag->save();
        if($result){
            return $this->success('保存成功','admin/index/taglist');
        }else if($result===0){
            return $this->error('没有进行修改，请重新输入');
        }else{
            return $this->error('保存失败');
        }

    }
}